//
//  WLFileRequest.m
//  WanLian
//
//  Created by 123456 on 2019/1/4.
//  Copyright © 2019 wanlian. All rights reserved.
//

#import "WLFileRequest.h"
#import "WLFileTransportHeadModel.h"
#import "WLMd5Tool.h"
#import "WLCommonTool.h"
//#define BLOCKSIZE   134217728        //128M
//#define BLOCKSIZE   268435456        //256M
#define BLOCKSIZE   536870912        //512M
//#define BLOCKSIZE    1073741824        //1G

@implementation WLFileRequest
- (NSString *)fileInfoDict {
    NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
    if (self.fileName.length > 0) {
        [infoDict setObject:self.fileName forKey:@"fileName"];
    }
    [infoDict setObject:[NSNumber numberWithLong:self.fileTotalBytes] forKey:@"total"];
    [infoDict setObject:[NSNumber numberWithLong:self.offset] forKey:@"offset"];
    [infoDict setObject:[NSNumber numberWithBool:self.bContinue] forKey:@"bContinue"];


    [infoDict setObject:[NSNumber numberWithInt:self.fileType] forKey:@"fileType"];
    if (self.reqeustURL.length > 0) {
        [infoDict setObject:self.reqeustURL forKey:@"filePath"];
    }
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:infoDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *infoStr = [[NSString alloc] initWithData:infoData encoding:NSUTF8StringEncoding];
    return infoStr;
}
- (NSString *)fileInfoDictWithBlockIndex:(NSInteger)index withSendData:(NSData *)sendData{
    if (index >= 0 && index < self.blockModelArr.count) {
        NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
        [infoDict setObject:self.fileName forKey:@"fileName"];
        [infoDict setObject:[NSNumber numberWithLong:self.fileTotalBytes] forKey:@"total"];
        WLFileTransportHeadModel *headModel = [self.blockModelArr objectAtIndex:index];
        long offset = headModel.offset;
        [infoDict setObject:[NSNumber numberWithLong:headModel.size] forKey:@"size"];
        NSString *md5Str = [WLMd5Tool MD5ForLower32BateWithData:sendData];
        if (md5Str.length > 0) {
            [infoDict setObject:md5Str forKey:@"mValue"];
        }
        [infoDict setObject:[NSNumber numberWithLong:offset] forKey:@"offset"];
        [infoDict setObject:[NSNumber numberWithBool:self.bContinue] forKey:@"bContinue"];
        [infoDict setObject:[NSNumber numberWithInt:headModel.freeid] forKey:@"freeid"];
        [infoDict setObject:[NSNumber numberWithInt:self.fileType] forKey:@"fileType"];
        if (self.reqeustURL.length > 0) {
            [infoDict setObject:self.reqeustURL forKey:@"filePath"];
        }
        NSData *infoData = [NSJSONSerialization dataWithJSONObject:infoDict options:NSJSONWritingPrettyPrinted error:nil];
        NSString *infoStr = [[NSString alloc] initWithData:infoData encoding:NSUTF8StringEncoding];
        return infoStr;
    }
    return nil;
}

- (void)fonfigBlockInfo {
    
    long blockSize = BLOCKSIZE;
    
    self.blockSize = blockSize;
    
    self.p_last_bs = self.fileTotalBytes - self.currentSize;
    self.blockCount = 1;
    
//    /*最后一个分块可能不足一个标准分块*/
//    int count = (int)(self.fileTotalBytes / blockSize);
//    if(self.fileTotalBytes%blockSize == 0){
//        self.blockCount = count;
//    }
//    else{
//        self.blockCount = count + 1;
//        self.p_last_bs = (long)(self.fileTotalBytes - blockSize*count);
//    }
}
- (NSData *)getTransmitDataWithSendData:(NSData *)sendData blockIndex:(NSInteger)index{
    int totallength = 0;
    int msglength = 0;
    int datalength = (int)sendData.length;
    
    NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
    [messageDict setObject:DEVICE_FILE_UPLOAD_REQ forKey:@"type"];
    NSString *fileInfoStr = [self fileInfoDictWithBlockIndex:index withSendData:sendData];
    if (fileInfoStr) {
        [messageDict setObject:fileInfoStr forKey:@"content"];
    }
    
    NSString *userTag = [[WLUseManager shareUser] getUserRouterTag];
    if (userTag) {
        [messageDict setObject:userTag forKey:@"clientId"];
    }
    NSString * tagClientId = [[WLUseManager shareUser] getUserCurrentRouter];
    if (tagClientId) {
        [messageDict setObject:tagClientId forKey:@"tagClientId"];
    }
    
    
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:messageDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *infoStr = [[NSString alloc] initWithData:infoData encoding:NSUTF8StringEncoding];
    NSData *resuInfoData = [infoStr dataUsingEncoding:NSUTF8StringEncoding];
    
    msglength = (int)resuInfoData.length;
    totallength = msglength + datalength;
    NSMutableData *headData = [[NSMutableData alloc]init];
    [headData appendInt:totallength];
    [headData appendInt:msglength];
    [headData appendInt:datalength];
    [headData appendData:resuInfoData];
    [headData appendData:sendData];
    return headData;
}

- (NSData *)getFileTransmitHeadDataWithType:(NSString *)type{
    int totallength = 0;
    int msglength = 0;
    int datalength = 0;
    NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
    [messageDict setObject:type forKey:@"type"];
    
    NSString *fileInfoStr = [self fileInfoDict];
    if (fileInfoStr) {
        [messageDict setObject:fileInfoStr forKey:@"content"];
    }
    NSString *userTag = [[WLUseManager shareUser] getUserRouterTag];
//    userTag = @"323232";
    if (userTag) {
        [messageDict setObject:userTag forKey:@"clientId"];
    }
//    NSString *tagClientId = @"d5c0bfaca2934f22bc3f8ff4b7b0dc97";
//    if (![WLCommonTool isConnectedTianYiLian])//([_routerSocket.connectedHost isEqualToString:HOST_ROUTER])
//    {
    NSString * tagClientId = [[WLUseManager shareUser] getUserCurrentRouter];
//    }
//    tagClientId = @"7a24e88976a9215f8ef76d6a6a4353a4";
    if (tagClientId) {
        [messageDict setObject:tagClientId forKey:@"tagClientId"];
    }
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:messageDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *infoStr = [[NSString alloc] initWithData:infoData encoding:NSUTF8StringEncoding];
    NSData *resuInfoData = [infoStr dataUsingEncoding:NSUTF8StringEncoding];
    msglength = (int)resuInfoData.length;
    totallength = msglength + datalength;
    NSMutableData *headData = [[NSMutableData alloc]init];
    [headData appendInt:totallength];
    [headData appendInt:msglength];
    [headData appendInt:datalength];
    [headData appendData:resuInfoData];
    
    return headData;
}

@end


@implementation WLFileRequestConfig

@end

